package com.alinu.web.listener;

import com.alinu.settings.domain.DicValue;
import com.alinu.settings.service.DicService;
import com.alinu.settings.service.impl.DicServiceImpl;
import com.alinu.utils.ServiceFactory;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.*;

/**
 * @author chenzuwei
 * @date 2021/3/18
 * @description 系统上下文域监听器
 */
public class SysInitListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext applicaion = servletContextEvent.getServletContext();

        DicService dicService = (DicService) ServiceFactory.getService(new DicServiceImpl());
        // 从数据字典中取得数据
        Map<String, List<DicValue>> dicValueMap = dicService.getValueByType();

        Set<String> typeSet = dicValueMap.keySet();

        for (String dicType : typeSet) {
            // 将数据按照类型存入缓存中
            applicaion.setAttribute(dicType, dicValueMap.get(dicType));
        }

        Map<String, String> stagePossiblity = new HashMap<>();
        // 处理Stage2Properties文件
        ResourceBundle rb = ResourceBundle.getBundle("Stage2Possibility");
        Enumeration<String> keys = rb.getKeys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            String value = rb.getString(key);
            stagePossiblity.put(key, value);
        }

        applicaion.setAttribute("stagePossibility", stagePossiblity);


    }

}
